package com.train.trainmanage.serviceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.train.trainmanage.mapper.DepartmentModelMapper;
import com.train.trainmanage.mapper.LocomotiveModelMapper;
import com.train.trainmanage.model.DepartmentModel;
import com.train.trainmanage.model.LocomotiveModel;
import com.train.trainmanage.service.LocomotiveService;

@Service
public class LocomotiveServiceImpl implements LocomotiveService{
	
	@Autowired
	private LocomotiveModelMapper locoMapper;
	@Autowired
	private DepartmentModelMapper departmentMapper;

	@Override
	public List<LocomotiveModel> listByLoco(LocomotiveModel record) {
		Map<String,Object> map = new HashMap<String,Object>();
		if(record != null) {
			if(record.getLocoName() != null && !"".equals(record.getLocoName())) {
				map.put("locoName", record.getLocoName());
			}
		}
		
		List<LocomotiveModel> locoList = locoMapper.listByMap(map);
		if(locoList != null && locoList.size() >0) {
			for(LocomotiveModel temLoco : locoList) {
				Integer departmentId = temLoco.getBearBureau();
				DepartmentModel department = departmentMapper.selectByPrimaryKey(departmentId);
				if(department != null) {
					temLoco.setBearBureauName(department.getDepartName());
				}
			}
		}
		
		return locoList;
	}

	@Override
	public int addLoco(LocomotiveModel record) {
		return locoMapper.insertSelective(record);
	}

	@Override
	public int updateLoco(LocomotiveModel record) {
		return locoMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public LocomotiveModel getByLocoId(Integer locoId) {
		return locoMapper.selectByPrimaryKey(locoId);
	}

	@Override
	public LocomotiveModel getByTrainId(Integer trainId) {
		LocomotiveModel loco = locoMapper.selectByTrainId(trainId);
		if(loco != null) {
			DepartmentModel department = departmentMapper.selectByPrimaryKey(loco.getBearBureau());
			loco.setBearBureauName(department.getDepartName());
		}
		return loco;
	}

}
